<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
</head>
<body>
    <button id="public">发布</button>
    <script>
        let public = document.querySelector('#public');

        //粉丝

        class Fan{
            constructor(name){
                this.name = name;
            }
            update(){
                console.log(`${this.name}接受到消息`)
            }
        }


        //明星

        class Star{
            constructor(){
                this.fans = []; //粉丝的实例
            }

            add(fan){
                this.fans.push(fan);
            }

            public(){
                this.fans.forEach(fan => fan.update())
            }

        }

        let f1 = new Fan('lili');

        let f2 = new Fan('zs');

        let f3 = new Fan('tom');


        let star = new Star();

        star.add(f1);

        star.add(f2);

        star.add(f3);

        public.addEventListener('click',() => {
            //明星发表动态
            star.public();
        })
    </script>
</body>
</html>